package exabase.com;

import java.io.IOException;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

import exabase.com.dataTransfer.DataTransfer;
import exabase.com.dataTransfer.PeerInfo;

public class Client extends Thread {
	private static Logger logger = Logger.getLogger(Client.class.getName());

	public void run() {
		while (true) {
			try {
				Socket socket = new Socket("localhost", 4444);

				DataTransfer dataTransfer = new DataTransfer(socket);
				PeerInfo myInfo = new PeerInfo(socket.getLocalAddress()
						.getHostName(), socket.getLocalPort());
				dataTransfer.sendAndGetPeerInfo(myInfo);
			} catch (UnknownHostException e) {
				logger.log(Level.WARNING, "Don't know about host: hostname", e);
			} catch (SocketTimeoutException e) {
				// Timeout, do nothing.
			} catch (IOException e) {
				logger.log(Level.WARNING,
						"Couldn't get I/O for the connection to: hostname", e);
			} catch (ClassNotFoundException e) {
				logger.log(Level.WARNING, "ClassNotFoundException", e);
			}

			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				logger.log(Level.WARNING, "Sleep interrupted", e);
			}
		}
	}
}
